<?php
		/*
		Plugin Name: MMWW
		Plugin URI: http://joebackward.wordpress.com/2012/11/25/mmww-media-metadata-workfilow-wizard-plugin-for-wordpress-3-4/
		Description: Use the Media Metadata Workflow Wizard to integrate media metadata workflow with Wordpress's Media Library. If you create lots of images, audio clips, or video clips you probably work hard to put metadata (titles, authors, copyrights, track names, dates, and all that) into them. Now you can have that metadata stored into the Media Library automatically when you upload your media files.
		Author: Ollie Jones
		Version: 0.9.1
		Author URI: http://joebackward.wordpress.com/2012/11/25/mmww-media-metadata-workfilow-wizard-plugin-for-wordpress-3-4/
		Text Domain: mmww
		*/
/** current version number  */
if (!defined('MMWW_VERSION_NUM')) {
	define('MMWW_VERSION_NUM', '0.9.1');
}
/* set up some handy globals */
if (!defined('MMWW_THEME_DIR')) {
	define('MMWW_THEME_DIR', ABSPATH . 'wp-content/themes/' . get_template());
}
if (!defined('MMWW_PLUGIN_NAME')) {
	define('MMWW_PLUGIN_NAME', trim(dirname(plugin_basename(__FILE__)), '/'));
}
if (!defined('MMWW_PLUGIN_DIR')) {
	define('MMWW_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . MMWW_PLUGIN_NAME);
}
if (!defined('MMWW_PLUGIN_URL')) {
	define('MMWW_PLUGIN_URL', WP_PLUGIN_URL . '/' . MMWW_PLUGIN_NAME);
}
if (!defined('MMWW_POSTMETA_KEY')) {
	define('MMWW_POSTMETA_KEY', '_' . MMWW_PLUGIN_NAME . '_metadata');
}

register_activation_hook( __FILE__, 'mmww_activate' );

add_action( 'init', 'mmww_do_everything' );

/* check version and upgrade plugin if need be. */
if (MMWW_VERSION_NUM != ($opt = get_option('mmww_version', '0.0.0'))) {
	/* do update procedure here as needed */
	update_option('mmww_version', MMWW_VERSION_NUM);
}

function mmww_do_everything () {
	mmww_augment_include_path(MMWW_PLUGIN_DIR . '/code');
	require_once( 'mmww_media_upload.php' );

	if ( is_admin() && current_user_can ( 'manage_options' )) {
		mmww_augment_include_path(MMWW_PLUGIN_DIR . '/code');
		require_once ( 'mmww_admin.php' );
	}
	if (current_user_can( 'upload_files' )) {
		add_filter('attachment_fields_to_edit', 'mmww_attachment_fields_to_edit',20,2);		
	}
}

/** filter stub for attachment loading fields
 *   
 *   Let's defer loading all that code until this is actually invoked
 *   to keep user overhead down.
 * @param array of fields $file
 */
function mmww_attachment_fields_to_edit($file,$post) {
	mmww_augment_include_path(MMWW_PLUGIN_DIR . '/code');
	//todo re-enable this require_once( 'mmww_media_upload.php' );
	return mmww_fields_to_edit($file, $post);	
}

function mmww_augment_include_path($addto) {
	$before = get_include_path();
	$found = FALSE;
	foreach(explode(';', $before) as $path) {
		if ($path == $addto) {
			$found = TRUE;
		}
	}
	if (!$found) {
		set_include_path($addto . ';' . $before);
	}
}

function mmww_activate() {
	if ( version_compare( get_bloginfo( 'version' ), '3.1', '<' ) ) {
		deactivate_plugins( basename( __FILE__ ) ); /* fail activation */
	}
	/* make sure the option is loaded, but don't overwrite it. */
	add_option('mmww_version', MMWW_VERSION_NUM);
}

?>